package com.bot.rf.entity;
/**
 * @program: report-form-demo
 * @description: 条形码工具类
 * @author: zyy
 * @create: 2021-02-25 13:09
 **/
public class BarcodeUtil {

    //--------------------------- BarcodeUtil解释 start -----------------------------
    //
    // 1、报表程序需要根据 tagId 得到一个参数
    // 2、该参数用于从数据库中查询图书的名称、索书号、馆藏状态等
    // 3、该参数可以是 barcode，也可以就是 tagId 本身，由馆内情况自定义
    //
    //--------------------------- BarcodeUtil解释  end  -----------------------------

    public static String epcToBarcode(String epc){
        if (epc.length() < 4 || !"0003".equals(epc.substring(0,4))){
            return "";
        }
        String first = getFirst(epc);
        String second = getSecond(epc);
        String third = getThird(epc);
        String forth = getForth(epc);
        String left = getLeft(epc);
        String last = getLast(epc);

        return first + second + third + forth + left + last;
    }

    private static String getLast(String epc) {
        String handle = epc.substring(8,9);
        String handle2 = epc.substring(11,12);
        String binary = hexString2binaryString(handle);
        String binary2 = hexString2binaryString(handle2);

        String combinedBinary = binary2 + binary;

        int val = Integer.parseInt(combinedBinary,2);
        return String.valueOf((char)val);

    }

    private static String getLeft(String epc) {
        String handle = epc.substring(16,18);
        String handle2 = epc.substring(18,24);
        String binary = hexString2binaryString(handle);
        String binary2 = hexString2binaryString(handle2);

        String part1 = binary2.substring(16,24);
        String part2 = binary2.substring(8,16);
        String part3 = binary2.substring(0,8);

        String combinedBinary = part1 + part2 + part3 + binary.substring(0,6);
        int val = Integer.parseInt(combinedBinary,2);
        return String.valueOf(val);

    }

    private static String getForth(String epc) {
        String handle = epc.substring(14,15);
        String handle2 = epc.substring(17,18);
        String binary = hexString2binaryString(handle);
        String binary2 = hexString2binaryString(handle2);

        String combinedBinary = binary2.substring(2,4) + binary.substring(0,2);

        int val = Integer.parseInt(combinedBinary,2);
        val += 48;
        return String.valueOf((char)val);
    }

    private static String getThird(String epc) {
        String handle = epc.substring(10,11);
        String handle2 = epc.substring(13,14);
        String binary = hexString2binaryString(handle);
        String binary2 = hexString2binaryString(handle2);

        String combinedBinary = binary2.substring(2,4) + binary;

        int val = Integer.parseInt(combinedBinary,2);
        val += 48;
        return String.valueOf((char)val);

    }

    private static String getSecond(String epc) {
        String handle = epc.substring(12,14);
        String binary = hexString2binaryString(handle);
        binary = binary.substring(0,6);
        int val = Integer.parseInt(binary,2);
        val += 48;
        return String.valueOf((char)val);
    }

    private static String getFirst(String epc) {
        String handle = epc.substring(14,16);
        String binary = hexString2binaryString(handle);
        binary = binary.substring(2);
        int val = Integer.parseInt(binary,2);
        val += 48;
        return String.valueOf((char)val);
    }

    private static String hexString2binaryString(String hexString) {
        if (hexString == null){
            return null;
        }
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000" + Integer.toBinaryString(Integer.parseInt(hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }

    public static void main(String[] args) {
//        System.out.println("000355".substring(0,4));
//        System.out.println(epcToBarcode("000300538E030001447572EA"));
//        System.out.println(epcToBarcode("000300538E030001447572EA").equals("10009833424178"));
//        System.out.println(epcToBarcode("000300536E030001487572EA").equals("10009833424186"));
    }
}
